﻿#include "MainWindow.hpp"
#include "../resource.h"
using namespace NAMESPACENAME;
MainWindow::MainWindow() :
	WindowForm(NAMESPACENAME::Rectangle(CW_USEDEFAULT, CW_USEDEFAULT, 1366, 768)),
	page(new NAMESPACENAME::Page(NAMESPACENAME::Rectangle(0, 0, 1700, 920), NAMESPACENAME::String(STRING("Page01")), this)),
	button(new NAMESPACENAME::Button(NAMESPACENAME::Rectangle(50, 50, 150, 150), NAMESPACENAME::String(STRING("Button")), page)),
	displayButton(new NAMESPACENAME::Button(NAMESPACENAME::Rectangle(50, 200, 150, 150), NAMESPACENAME::String(STRING("Visable")), page)),
	label(new NAMESPACENAME::Label(NAMESPACENAME::Rectangle(200, 50, 150, 150), NAMESPACENAME::String(STRING("Label")), page)),
	richEdit(new NAMESPACENAME::RichEdit(NAMESPACENAME::Rectangle(50, 350, 300, 150), NAMESPACENAME::String(STRING("RichEdit")), page))
{
	pages.push_back(page);
	//Restore Size And Position
	{
		std::string ret(ConfigFile::configFile["LastWindowStatus"]);
		if (ret != "" && ret != "Hidden")
		{
			MoveWindow(
				hWnd,
				atoi(ConfigFile::configFile["X"].c_str()),
				atoi(ConfigFile::configFile["Y"].c_str()),
				atoi(ConfigFile::configFile["Width"].c_str()),
				atoi(ConfigFile::configFile["Height"].c_str()),
				false
			);
			if (ret == "Maximize")
				Visibility(Control::Visibility::Maximize);
			else
				Visibility(Control::Visibility::Visible);
		}
		else
			Visibility(Control::Visibility::Visible);
	}
	onDestroy += SaveWindowPositionAndSize;
	//Set Logo
	{
		HANDLE icon = LoadImageW(Application::GetInstance(), MAKEINTRESOURCEW(IDI_NETLOGOLOGO), IMAGE_ICON, 512, 512, 0);
		SendMessageW(hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon);
		SendMessageW(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon);
	}

	// Button Test Passed
	button->onMouseClick += &MainWindow::ButtonClick;
	button->Visibility(Control::Visibility::Visible);

	displayButton->onMouseClick += &LeftButtonClick;
	displayButton->Visibility(Control::Visibility::Visible);
	// Label Test Not Passed: Problem with Clicking
	// May be the problem of windows
	label->onMouseClick += &MainWindow::LabelClick;
	label->Visibility(Control::Visibility::Visible);
	// Click on other space of window

	//RichEdit
	richEdit->Visibility(Control::Visibility::Visible);

	page->Visibility(Visibility::Visible);
}
void MainWindow::LeftButtonClick(Control* sender, NAMESPACENAME::EventArgs& e)
{
	MainWindow* win(
		dynamic_cast<MainWindow*>(
			dynamic_cast<NAMESPACENAME::Page*>(
				dynamic_cast<NAMESPACENAME::Button*>(sender)->Parent()
				)->Parent()
			)
	);
	win->label->Visibility(Visibility::Visible);
	win->button->Visibility(Visibility::Visible);
}
void MainWindow::SaveWindowPositionAndSize(Control* sender, EventArgs& e)
{
	if (sender->Visibility() == Visibility::Hidden)
	{
		ConfigFile::configFile.SetValue("LastWindowStatus", "Hidden");
		ConfigFile::configFile.Remove("X");
		ConfigFile::configFile.Remove("Y");
		ConfigFile::configFile.Remove("Width");
		ConfigFile::configFile.Remove("Height");
		return;
	}
	RECT rct;
	GetWindowRect(sender->WindowHandle(), &rct);
	if (sender->Visibility() == Visibility::Maximize)
		ConfigFile::configFile.SetValue("LastWindowStatus", "Maximize");
	else if (sender->Visibility() == Visibility::Visible)
		ConfigFile::configFile.SetValue("LastWindowStatus", "Windowed");
	NAMESPACENAME::Rectangle rect(rct);
	ConfigFile::configFile.SetValue("X", std::to_string(rect.x).c_str());
	ConfigFile::configFile.SetValue("Y", std::to_string(rect.y).c_str());
	ConfigFile::configFile.SetValue("Width", std::to_string(rect.width).c_str());
	ConfigFile::configFile.SetValue("Height", std::to_string(rect.height).c_str());
}